﻿--[[
   @brief    GroupBanner
   @author   vincentlao
   @version  1.0
   @date     2015-06-11
--]]

ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");
ierd_tgp.tpf_ui.Require("social\\group_message\\model.lua");
ierd_tgp.tpf_ui.Require("social\\utility.lua");

GroupBanner = {
};

GroupBannerUIEventListener = {
};

function GroupBanner:new (group_id, wrapper_id)
    ierd_tgp.tpf_ui.Log(string.format("GroupGroupBanner:new %s %d", group_id, wrapper_id));

    local ui_str, str_holder = U8toCUIStr("FriendBannerBk");
    local bk_id = ui_utility:uilib():GetControlIDbyPath(ui_str, wrapper_id);

    local o = {group_id_ = group_id, wrapper_id_ = wrapper_id, bk_id_ = bk_id}
    setmetatable(o, self)
    self.__index = self
    o:init()
    return o
end

function GroupBanner:init()
    self.uilib_ = ui_utility:uilib();
    self.selected_ = false;

    self:update_info();
end

function GroupBanner:update(group_id)
    self.group_id_ = group_id;
    self:update_info();
end

function GroupBanner:update_info()
    self:update_basic_info();
    self:update_mask_info();
    self:update_message_count();
    self:update_message_info();
end

function GroupBanner:update_basic_info()
    local group_info = group_list_model.get_group_info(self.group_id_);
    if group_info ~= nil then
        --basic info

        local properity_head = string.format("ONEIMAGE:social_network\\group-head-small.png|%s;Circle;null;", group_info["face_url"]);
        local ui_str, str_holder = U8toCUIStr("Button_Head_Image");
        local head_id = self.uilib_:GetControlIDbyPath(ui_str, self.wrapper_id_);
        ui_utility.setproperty_by_id(head_id, "Background", properity_head);
        base_utility.log(string.format("group head url =  %s", properity_head));

        MainCommon.set_static_text(self.uilib_, self.wrapper_id_, "Static_Group_Name", group_info.name);

        --game icon 
        --[[   
        local game_id = 0;
        if group_info.ext_data2 ~= nil and group_info.ext_data2 ~= '' then
            local game_info_tbl = base_utility.json_to_table(group_info.ext_data2);
            if game_info_tbl and game_info_tbl.game then
                game_id = tonumber(game_info_tbl.game);
            end
        end
   
        if game_id ~= nil then
            local game_info = banner_model.get_game_base_info(game_id);
            self:update_game_icon(game_info);            
        end]]
    end
end

function GroupBanner:update_mask_info()
    local group_info = group_list_model.get_group_info(self.group_id_);
    if group_info ~= nil then
        local ui_str, str_holder = U8toCUIStr("Button_Mask_Image");
        local mask_id = self.uilib_:GetControlIDbyPath(ui_str, self.wrapper_id_);
        if group_info.mask_msg_flag == 2 then
            MainCommon.set_visible(self.uilib_, mask_id, true);
        else
            MainCommon.set_visible(self.uilib_, mask_id, false)
        end
    end
end


function GroupBanner:update_message_count()
    local ui_str, str_holder = U8toCUIStr("Button_New_Message");
    local message_ctrl_id = self.uilib_:GetControlIDbyPath(ui_str, self.wrapper_id_);
    ui_str, str_holder = U8toCUIStr("Button_New_Message_Big");
    local message_big_ctrl_id = self.uilib_:GetControlIDbyPath(ui_str, self.wrapper_id_);

    count = group_list_model.get_group_history_unread_msg_count(self.group_id_);

    if count == 0 then
        MainCommon.set_visible(self.uilib_, message_ctrl_id, false);
        MainCommon.set_visible(self.uilib_, message_big_ctrl_id, false);
    elseif count < 10 then
        MainCommon.set_visible(self.uilib_, message_ctrl_id, true);
        MainCommon.set_visible(self.uilib_, message_big_ctrl_id, false);
        MainCommon.set_button_caption(self.uilib_, self.wrapper_id_, "Button_New_Message", string.format("%d", count));
    elseif count < 100 then
        MainCommon.set_visible(self.uilib_, message_ctrl_id, false);
        MainCommon.set_visible(self.uilib_, message_big_ctrl_id, true);
        MainCommon.set_button_caption(self.uilib_, self.wrapper_id_, "Button_New_Message_Big", string.format("%d", count));
    else
        MainCommon.set_visible(self.uilib_, message_ctrl_id, false);
        MainCommon.set_visible(self.uilib_, message_big_ctrl_id, true);
        MainCommon.set_button_caption(self.uilib_, self.wrapper_id_, "Button_New_Message_Big", "99+");
    end
end

function GroupBanner:update_message_info()
    local last_msg = group_list_model.get_last_msg(self.group_id_);
    local last_msg_in_push = group_message_model.get_group_last_message(self.group_id_);
    
    if last_msg == nil and last_msg_in_push ~= nil then
        last_msg = last_msg_in_push;
    end

    if last_msg == nil then
        MainCommon.set_visible(self.uilib_, MainCommon.get_child_id(self.uilib_, self.wrapper_id_, "Static_Message_Time"), false);
        MainCommon.set_visible(self.uilib_, MainCommon.get_child_id(self.uilib_, self.wrapper_id_, "Static_Group_Message"), false);
        return;
    end

    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");

    MainCommon.set_visible(self.uilib_, MainCommon.get_child_id(self.uilib_, self.wrapper_id_, "Static_Online"), false);
    MainCommon.set_visible(self.uilib_, MainCommon.get_child_id(self.uilib_, self.wrapper_id_, "Button_Online_Image"), false);

    MainCommon.set_visible(self.uilib_, MainCommon.get_child_id(self.uilib_, self.wrapper_id_, "Static_Message_Time"), true);
    local formated_time_t = ierd_tgp.social_network.tagFormatedTime:new();
    social_network:format_time(last_msg.time, formated_time_t);
    MainCommon.set_static_text(self.uilib_, self.wrapper_id_, "Static_Message_Time", formated_time_t.time_fromated);
    formated_time_t:delete();

    MainCommon.set_visible(self.uilib_, MainCommon.get_child_id(self.uilib_, self.wrapper_id_, "Static_Group_Message"), true);

    local msg_text = social_utility.format_msg(last_msg.message);

    if last_msg.msg_type == 1 then
        msg_text = social_utility.format_discussion_msg(last_msg);
        if msg_text == nil then
            msg_text = LoadStr("SOCIAL_GROUP_LIST_FREASH");
        end
    end

    if msg_text == nil then
        msg_text = "";
    end

    MainCommon.set_static_text(self.uilib_, self.wrapper_id_, "Static_Group_Message", msg_text);
end

function GroupBanner:update_game_icon(game_info)
    if game_info == nil then
        base_utility.log(string.format("[GroupBanner][update_game_icon]game_info == nil"), true);
        return;
    end
    base_utility.log(string.format("[GroupBanner][update_game_icon]game_info.icon_url = %s", game_info.icon_url), true);
    local pro_value = string.format("ONEIMAGE:social_network\\game.png|%s;Stretch;null;", game_info.mini_icon_url);
    local ui_str, str_holder = U8toCUIStr("Button_Game_Image");
    local game_image_id = self.uilib_:GetControlIDbyPath(ui_str, self.wrapper_id_);
   ui_utility.setproperty_by_id(game_image_id, 'Background', pro_value);
end

function GroupBanner:set_selected(selected)
    self.selected_ = selected;
end

function GroupBanner:get_selected()
    return self.selected_;
end

function GroupBanner:log(msg, core_msg)
    base_utility.log(string.format("[GroupBanner][%d] %s", self.game_id_, msg), core_msg);
end

function GroupBanner:get_element(name_path, interface_type)
    self:log(string.format("get_element, %s(%s)", name_path, interface_type));
    return MainCommon.get_element(self.uilib_, self.ctrl_id_, name_path, interface_type);
end

function GroupBanner:set_normal_state()
   ui_utility.setproperty_by_id(self.bk_id_, "Background", "");
end

function GroupBanner:set_hover_state()
   ui_utility.setproperty_by_id(self.bk_id_, "Background", "9IMAGES:social_network\\item-state.png?13,15,19,21;social_network\\item-state.png?25,15,35,21;social_network\\item-state.png?41,15,47,21;social_network\\item-state.png?13,27,19,37;social_network\\item-state.png?25,27,35,37;social_network\\item-state.png?41,27,47,37;social_network\\item-state.png?13,43,19,49;social_network\\item-state.png?25,43,35,49;social_network\\item-state.png?41,43,47,49;Stretch;0;");
end

function GroupBanner:set_selected_state()
   ui_utility.setproperty_by_id(self.bk_id_, "Background", "9IMAGES:social_network\\item-state.png?70,14,76,20;social_network\\item-state.png?82,14,92,20;social_network\\item-state.png?98,14,104,20;social_network\\item-state.png?70,27,76,37;social_network\\item-state.png?82,27,92,37;social_network\\item-state.png?98,27,104,37;social_network\\item-state.png?70,43,76,49;social_network\\item-state.png?82,43,92,49;social_network\\item-state.png?98,43,104,49;Stretch;0;");
end

function GroupBanner:get_group_id()
    return self.group_id_;
end

function GroupBanner:get_root_id()
    return self.wrapper_id_;
end
-------------------------------------------------------------------------------------

function GroupBannerUIEventListener.start_show_group_tip_timer()
    base_utility.log(string.format("[GroupBannerUIEventListener]start_show_group_tip_timer"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('GroupBannerUIEventListener.show_group_tip_timer_process');
    tpfui:AddInterval('GroupBannerUIEventListener.show_group_tip_timer_process', 300);
end

function GroupBannerUIEventListener.stop_show_group_tip_timer()
    base_utility.log(string.format("[GroupBannerUIEventListener]stop_show_group_tip_timer"));
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('GroupBannerUIEventListener.show_group_tip_timer_process');
end

function GroupBannerUIEventListener.show_group_tip_timer_process()
    GroupBannerUIEventListener.stop_show_group_tip_timer();
    GroupBannerUIEventListener.send_show_group_tips_event();
end

function GroupBannerUIEventListener.send_show_group_tips_event()
    base_utility.log(string.format("[GroupBannerUIEventListener]send_show_group_tips_event"));
    local pControl =ui_utility.get_ctrl_ptr_by_id(GroupBannerUIEventListener.current_hover_header_ctrl_id);
    local group_banner = GroupBannerList:get_banner_by_ctrl(GroupBannerUIEventListener.current_hover_header_ctrl_id);
    local rc = pControl:GetRectEx(TRUE,TRUE);
    local param = {};
    param["bShow"] = true;    
    param["group_id"] = group_banner:get_group_id();
    param["x"] = rc.left;
    param["y"] = rc.top;
    event_center.send_event(group_event.SHOW_GROUP_TIPS,base_utility.table_to_json(param));
end
function GroupBannerUIEventListener.send_hide_group_tips_event(action)
    base_utility.log(string.format("[GroupBannerUIEventListener]send_hide_group_tips_event:%d", action));
    local param = {};
    param["bShow"] = false;
    param["action"] = action;
    event_center.send_event(group_event.SHOW_GROUP_TIPS,base_utility.table_to_json(param));
end
---------
function GroupBannerUIEventListener.mouse_over_group_imgage(uilib, control_id)
    if group_menu_view == nil or group_menu_view.is_show() == false then
        GroupBannerUIEventListener.current_hover_header_ctrl_id = control_id;
        GroupBannerUIEventListener.stop_show_group_tip_timer();
        GroupBannerUIEventListener.start_show_group_tip_timer();
    end

    local banner = GroupBannerList:get_banner_by_ctrl(control_id);
    if banner == nil then
        base_utility.log(string.format("[GroupBannerUIEventListener][mouse_over_group]banner is NIL,"), true);
        return;
    end
    
    local selected = banner:get_selected();
    if  selected == false then 
      banner:set_hover_state();
    end
end

function GroupBannerUIEventListener.mouse_leave_group_imgage(uilib, control_id)
    base_utility.log("[GroupBannerUIEventListener] mouse_leave_group_imgage");
    GroupBannerUIEventListener.stop_show_group_tip_timer();

    if group_menu_view == nil or group_menu_view.is_show() == false then
        GroupBannerUIEventListener.send_hide_group_tips_event(0);
    end

    local banner = GroupBannerList:get_banner_by_ctrl(control_id);
    if banner == nil then
        base_utility.log(string.format("[GroupBannerUIEventListener][mouse_over_group]banner is NIL,"), true);
        return;
    end
    
    local selected = banner:get_selected();
    if  selected == false then 
      banner:set_normal_state();
    end
end

function GroupBannerUIEventListener.on_left_button_down_image(uilib, control_id, x, y)
    base_utility.log("[GroupBannerUIEventListener] on_left_button_down_image");
    GroupBannerList:check_group_by_banner_id(MainCommon.get_parent_id(uilib, control_id));

    local pControl =ui_utility.get_ctrl_ptr_by_id(control_id);
    local banner = GroupBannerList:get_banner_by_ctrl(control_id);
    local param_table = {group_id = banner:get_group_id()};
    event_center.send_event(group_event.SHOW_GROUP_CENTER,base_utility.table_to_json(param_table));

    GroupBannerUIEventListener.send_hide_group_tips_event(1);
end

function GroupBannerUIEventListener.on_right_button_down_image(uilib, control_id, x, y)
    GroupBannerList:check_group_by_banner_id(MainCommon.get_parent_id(uilib, control_id));
end

function GroupBannerUIEventListener.on_right_button_up_image(uilib, control_id, x, y)
    local rc = uilib:GetControlPtr(control_id):GetRectEx(FALSE,TRUE);
    local p_ctrl = uilib:GetControlPtr(control_id):GetParent();
    local p_id = p_ctrl:GetControlID();
    GroupBannerUIEventListener.on_right_button_up(uilib, p_id, rc.left + x, rc.top + y);
end

function GroupBannerUIEventListener.mouse_over_group(uilib, control_id)
    local banner = GroupBannerList:get_banner_by_wrapper(control_id);
    if banner == nil then
        base_utility.log(string.format("[GroupBannerUIEventListener][mouse_over_group]banner is NIL,"), true);
        return;
    end
    
    local selected = banner:get_selected();
    if  selected == false then 
      banner:set_hover_state();
    end
end

function GroupBannerUIEventListener.mouse_leave_group(uilib, control_id)
    local banner = GroupBannerList:get_banner_by_wrapper(control_id);
    if banner == nil then
        base_utility.log(string.format("[GroupBannerUIEventListener][mouse_over_group]banner is NIL,"), true);
        return;
    end
    
    local selected = banner:get_selected();
    if  selected == false then 
      banner:set_normal_state();
    end
end

function GroupBannerUIEventListener.on_right_button_up(uilib, control_id, x, y)
    local group_banner = GroupBannerList:get_banner_by_wrapper(control_id);
    local pControl =ui_utility.get_ctrl_ptr_by_id(control_id);
    local rc = pControl:GetRectEx(TRUE,TRUE);
    local param = {};
    param["group_id"] = group_banner:get_group_id();
    param["x"] = rc.left + x;
    param["y"] = rc.top + y;
    event_center.send_event(group_event.SHOW_GROUP_MENU,base_utility.table_to_json(param))
    
    GroupBannerUIEventListener.send_hide_group_tips_event(1);
end

function GroupBannerUIEventListener.on_left_button_down(uilib, control_id, x, y)
    GroupBannerList:check_group_by_banner_id(control_id);
end

function GroupBannerUIEventListener.on_right_button_down(uilib, control_id, x, y)
    if control_id == nil then
        return;
    end
    
    GroupBannerList:check_group_by_banner_id(control_id);
end

--发送消息
function GroupBannerUIEventListener.mouse_db_click(uilib, control_id)
    local group_banner = GroupBannerList:get_banner_by_wrapper(control_id);

    local table_param = {};
    local groups = {};

    local info = {};
    info.id = group_banner:get_group_id();
    info.is_new = false;
    ierd_tgp.tpf_ui.Require("social\\group_list\\model.lua");
    info.time = group_list_model.get_last_msg_time(info.id);    
    info.group_type = group_list_model.get_group_type(info.id);
    table.insert(groups, info);
    
    table_param["groups"] = groups;

    event_center.send_event(group_event.SHOW_TALK_DIALOG,base_utility.table_to_json(table_param));
end
